<template>
  <div class="network-settings-container">
    <div class="header">
      <button class="back-btn" @click="goBack">
        <el-icon><ArrowLeft /></el-icon>
      </button>
      <h1 class="title">网络设置</h1>
      <div class="connection-status">
        <span :class="['status-indicator', carStore.statusClass]"></span>
        <span class="status-text">{{ carStore.statusText }}</span>
      </div>
    </div>

    <div class="content">
      <div class="settings-card">
        <h3 class="card-title">连接配置</h3>
        
        <div class="form-group">
          <label class="form-label">IP地址</label>
          <input 
            v-model="form.ip" 
            type="text" 
            class="input-field"
            placeholder="请输入IP地址"
            :disabled="carStore.connectionStatus === 'connecting'"
          />
        </div>

        <div class="form-group">
          <label class="form-label">TCP端口</label>
          <input 
            v-model.number="form.tcpPort" 
            type="number" 
            class="input-field"
            placeholder="请输入TCP端口"
            :disabled="carStore.connectionStatus === 'connecting'"
          />
        </div>

        <div class="form-group">
          <label class="form-label">视频端口</label>
          <input 
            v-model.number="form.videoPort" 
            type="number" 
            class="input-field"
            placeholder="请输入视频端口"
            :disabled="carStore.connectionStatus === 'connecting'"
          />
        </div>

        <div class="form-actions">
          <button 
            class="btn-primary connect-btn" 
            @click="connect"
            :disabled="carStore.connectionStatus === 'connecting'"
          >
            <span v-if="carStore.connectionStatus === 'connecting'" class="loading"></span>
            <span v-else>{{ carStore.isConnected ? '重新连接' : '连接' }}</span>
          </button>
          
          <button 
            v-if="carStore.isConnected"
            class="btn-secondary disconnect-btn" 
            @click="disconnect"
            :disabled="carStore.connectionStatus === 'connecting'"
          >
            断开连接
          </button>
        </div>
      </div>

      <div class="info-card">
        <h3 class="card-title">连接信息</h3>
        <div class="info-item">
          <span class="info-label">当前IP:</span>
          <span class="info-value">{{ carStore.networkConfig.ip }}</span>
        </div>
        <div class="info-item">
          <span class="info-label">TCP端口:</span>
          <span class="info-value">{{ carStore.networkConfig.tcpPort }}</span>
        </div>
        <div class="info-item">
          <span class="info-label">视频端口:</span>
          <span class="info-value">{{ carStore.networkConfig.videoPort }}</span>
        </div>
        <div class="info-item">
          <span class="info-label">连接状态:</span>
          <span :class="['info-value', carStore.statusClass]">
            {{ carStore.statusText }}
          </span>
        </div>
      </div>

      <div class="help-card">
        <h3 class="card-title">使用说明</h3>
        <div class="help-content">
          <p>1. 确保小车已开启并连接到同一网络</p>
          <p>2. 输入小车的IP地址和端口号</p>
          <p>3. 点击连接按钮建立通信</p>
          <p>4. 连接成功后即可进行小车控制</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useCarStore } from '../stores/car'
import { commonApi } from '../utils/api'
import { ArrowLeft } from '@element-plus/icons-vue'

const router = useRouter()
const carStore = useCarStore()

// 表单数据
const form = reactive({
  ip: '192.168.1.11',
  tcpPort: 6000,
  videoPort: 6500
})

// 页面加载时初始化表单
onMounted(() => {
  form.ip = carStore.networkConfig.ip
  form.tcpPort = carStore.networkConfig.tcpPort
  form.videoPort = carStore.networkConfig.videoPort
})

// 返回上一页
const goBack = () => {
  router.back()
}

// 连接小车
const connect = async () => {
  // 表单验证
  if (!form.ip) {
    commonApi.showError('请输入IP地址')
    return
  }
  
  if (!form.tcpPort || form.tcpPort < 1 || form.tcpPort > 65535) {
    commonApi.showError('请输入有效的TCP端口号')
    return
  }
  
  if (!form.videoPort || form.videoPort < 1 || form.videoPort > 65535) {
    commonApi.showError('请输入有效的视频端口号')
    return
  }

  const config = {
    ip: form.ip,
    tcpPort: form.tcpPort,
    videoPort: form.videoPort
  }

  const result = await carStore.configNetwork(config)
  
  if (result.success) {
    commonApi.showSuccess('连接成功')
  } else {
    commonApi.showError(result.message)
  }
}

// 断开连接
const disconnect = async () => {
  await carStore.disconnect()
  commonApi.showInfo('已断开连接')
}
</script>

<style scoped>
.network-settings-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 20px;
  background: linear-gradient(135deg, #f5f3ff 0%, #e9d5ff 100%);
}

.header {
  display: flex;
  align-items: center;
  margin-bottom: 32px;
  gap: 16px;
}

.back-btn {
  background: rgba(255, 255, 255, 0.8);
  border: none;
  border-radius: 12px;
  padding: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
}

.back-btn:hover {
  background: rgba(255, 255, 255, 0.9);
  transform: translateY(-2px);
}

.title {
  flex: 1;
  font-size: 24px;
  font-weight: 700;
  color: var(--text-primary);
  margin: 0;
}

.connection-status {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 20px;
  backdrop-filter: blur(10px);
}

.status-text {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
}

.content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 24px;
  overflow-y: auto;
}

.settings-card, .info-card, .help-card {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 24px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: var(--shadow-light);
}

.card-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 20px 0;
}

.form-group {
  margin-bottom: 20px;
}

.form-label {
  display: block;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 8px;
}

.input-field {
  width: 100%;
  background: rgba(255, 255, 255, 0.8);
  border: 2px solid var(--border-color);
  border-radius: 12px;
  padding: 12px 16px;
  font-size: 16px;
  transition: all 0.3s ease;
}

.input-field:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.1);
}

.input-field:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.form-actions {
  display: flex;
  gap: 12px;
  margin-top: 24px;
}

.connect-btn, .disconnect-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 24px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.connect-btn:disabled, .disconnect-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
}

.info-item:last-child {
  border-bottom: none;
}

.info-label {
  font-size: 14px;
  color: var(--text-secondary);
}

.info-value {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
}

.help-content {
  color: var(--text-secondary);
  line-height: 1.6;
}

.help-content p {
  margin: 8px 0;
  font-size: 14px;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .network-settings-container {
    padding: 16px;
  }
  
  .title {
    font-size: 20px;
  }
  
  .form-actions {
    flex-direction: column;
  }
  
  .connect-btn, .disconnect-btn {
    width: 100%;
  }
}

/* 横屏适配 */
@media screen and (orientation: landscape) {
  .network-settings-container {
    padding: 16px 24px;
  }
  
  .content {
    flex-direction: row;
    gap: 20px;
  }
  
  .settings-card {
    flex: 1;
  }
  
  .info-card, .help-card {
    flex: 1;
  }
}
</style> 